const Models = require('./../../models')

class CarManager{
    static async addCarManager(ctx){
        const req = ctx.request.body;
        if (req.name && req.position && req.age && req.iphone && req.identity){
            const data = await Models.carmanagers.create({
                name: req.name,
                position: req.position,
                age: req.age,
                iphone: req.iphone,
                identity: req.identity
            });
            ctx.response.status = 200;
            ctx.response.body = {
                code : 200,
                msg: '数据请求成功',
                data
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 401,
                msg: '请求数据不完整或者数据格式错误'
            }
        }
    }

    static async getCarManager(ctx){
        const data = await Models.carmanagers.findAll();
        ctx.response.status = 200;
        ctx.response.body = {
            code : 200,
            msg: '数据请求成功',
            data
        }
    }

    static async editCarManager(ctx){
        const req = ctx.request.body;
        const {id,name,position,iphone,age,identity} = req;
        const data = await Models.carmanagers.update({name,position,iphone,age,identity},{
            where:{
                id
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据编辑成功',
            data
        }
    }

    static async deleteCarManager(ctx){
        const req = ctx.request.body;
        const {id} = req;
        const data = await Models.carmanagers.destroy({
            where:{
                id
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据删除成功'
        }

    }
}

module.exports = CarManager;